﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using ZhCun.Framework.Common.Models.TableModel;
using ZhCun.Framework.Common.Models.ProcedureModel;

namespace ZhCun.Framework.DataAccess.DAL
{
   public interface IDBOperate
    {
        /// <summary>
        /// 增加多条记录返回所影响行数,如果Field为Null则增加去除属性为Null的所有字段,
        /// 如果sqlWhere为null则按主键更新一条记录,否则按条件来更新
        /// </summary>
       int Add<TModel>(Guid tranId, params TModel[] models);
        /// <summary>
        /// 更新多条记录,指定字段(fields)
        /// </summary>
       int Update<TModel>(Guid tranId, string[] fields, string sqlWhere, Dictionary<string, object> dbParams, params TModel[] modelList);
        /// <summary>
        /// 更新多条记录,指定忽略字段(ignoreFields)
        /// </summary>
       int UpdateByIgnoreField<TModel>(Guid tranId, string[] ignoreFields, string sqlWhere, Dictionary<string, object> dbParams, params TModel[] models);
        /// <summary>
        /// 根据主键值删除多条记录
        /// </summary>
        int Delete(Guid tranId, params object[] pkValues);
        /// <summary>
        /// 执行一条SQL语句,返回所影响行数,如果SQL包含参数值(@参数名),则dbParams必须赋值
        /// </summary>
        int ExecuteBySQL(Guid tranId, string sqlText, Dictionary<string, object> dbParams);
        /// <summary>
        /// 使用事务执行sql语句错误返回false
        /// </summary>
        /// <param name="transList">要执行sql语句的文本和参数(Key:sql文本,Value:参数)</param>
        /// <returns></returns>
        bool ExecSqlTran(Dictionary<string, Dictionary<string, object>> transList);
        /// <summary>
        /// 如果增删改或其他自定义方法加入了事务那么此方法开始正式提交事务执行.
        /// </summary>
        /// <returns>如果有错误则返回false,否则返回true</returns>
        bool ExecCurrentTrans();
        /// <summary>
        ///  判断某字段值是否存在
        ///  如果主键值为null则不考虑主键值,否则主键以外的记录中筛选
        /// </summary>        
        bool IsExist(object pkeyValue, string fieldName, object fieldValue, string sqlWhere, Dictionary<string, object> dbParams);
        /// <summary>
        /// 获取指定字段的值,根据主键值
        /// </summary>
        object GetObjectByField(string fieldName, object pkValue);
        /// <summary>
        /// 通过主键得到一个实体对象
        /// </summary>
        TModel GetModel<TModel>(object id);
        /// <summary>
        /// 获取记录数
        /// </summary>
        int GetRecordCount(string sqlWhere, Dictionary<string, object> dbParams);
        /// <summary>
        /// 获得非分页数据的DataTable,指定要显示的字段,指定where条件及参数,排序字段,和升序降序
        /// 如果displayFields为null,则查询所有字段
        /// </summary>
        DataTable GetDataTable(string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields);
        /// <summary>
        /// 获得分页数据,如果orderField为null则使用主键,如果orderField多个值,第一个位排序关键字段,其它依次排序
        /// 如果displayFields为null,则查询所有字段
        /// </summary>
        DataTable GetDataTableByPage(int onePageCount, int pageNo, bool isDesc, string[] orderField, out int pageCount, out int recordCount,
            string sqlWhere, Dictionary<string, object> dbParams, params string[] selectFields);
        /// <summary>
        /// 获取前几行数据
        /// </summary>
        DataTable GetDataTableTopCount(int topCount, string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields);
        /// <summary>
        /// 根据SQL语句得到DataTable,SQL语句中若包含参数(@参数) 则必须创建dbParams
        /// </summary>
        DataTable GetDataTableBySQL(string sqlText, Dictionary<string, object> dbParams);
        /// <summary>
        /// 获得非分页数据的DataTable,指定where条件及参数,排序字段,和升序降序
        /// </summary>
        DbDataReader GetDataReader(string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields);
        /// <summary>
        /// 获得分页数据,如果orderField为null则使用主键,如果orderField多个值,第一个位排序关键字段,其它依次排序
        /// </summary>
        DbDataReader GetDataReaderByPage(int onePageCount, int pageNo, bool isDesc, string[] orderField, out int pageCount, out int recordCount,
            string sqlWhere, Dictionary<string, object> dbParams, params string[] selectFields);
        /// <summary>
        /// 获取前几行数据
        /// </summary>
        DbDataReader GetDataReaderTopCount(int topCount, string sqlWhere, Dictionary<string, object> param, string[] orderFields, bool isDesc, params string[] selectFields);
        /// <summary>
        /// 根据SQL语句得到DataTable,SQL语句中若包含参数(@参数) 则必须创建dbParams
        /// </summary>
        DbDataReader GetDataReaderBySQL(string sqlText, Dictionary<string, object> dbParams);
        /// <summary>
        /// 获得非分页数据的List集合,指定where条件及参数,排序字段,和升序降序
        /// </summary>
        List<TModel> GetList<TModel>(string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields);
        /// <summary>
        /// 获得分页数据的List集合,如果orderField为null则使用主键,如果orderField多个值,第一个位排序关键字段,其它依次排序
        /// </summary>
        List<TModel> GetListByPage<TModel>(int onePageCount, int pageNo, bool isDesc, string[] orderField, out int pageCount, out int recordCount,
            string sqlWhere, Dictionary<string, object> dbParams, params string[] selectFields);
        /// <summary>
        /// 获取前几行数据的List集合
        /// </summary>
        List<T> GetListTopCount<T>(int topCount, string sqlWhere, Dictionary<string, object> param, string[] orderFields, bool isDesc, params string[] selectFields) ;
        /// <summary>
        /// 执行存储过程返回所影响行数
        /// </summary>
        /// <param name="procObj">存储过程实体对象</param>
        int ExecProcedureGetNon(ProcedureObjBase procObj);
        /// <summary>
        /// 执行存储过程返回DataTable
        /// </summary>
        DataTable ExecProcedureGetDataTable(ProcedureObjBase procObj);
        /// <summary>
        /// 执行存储过程返回首行首列
        /// </summary>
        object ExecProcedureGetObj(ProcedureObjBase procObj);
    }
}
